﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;

namespace DLV.BackEnd.Pages
{
    public partial class ChietTinhBaoHiem : AdminPageBase
    {
        protected List<CalculatorInfo> InsuranceList = new List<CalculatorInfo>();
        protected decimal TotalInsurancePrices = 0;
        protected decimal TotalInsurancePricesAdults = 0;
        protected decimal TotalInsurancePricesChild = 0;
        protected decimal TotalInsurancePricesBaby = 0;

        private List<CalculatorInfo> _calculatorList = new List<CalculatorInfo>();
        private readonly CalculatorController _calculatorController = new CalculatorController();
        protected TourInfo tourInfo = new TourInfo();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["TourId"]))
            {
                _calculatorList =
                    _calculatorController.ListCalculator()
                        .Where(p => p.TourId == int.Parse(Request.QueryString["TourId"])).ToList();
                tourInfo = new TourController().GetTour(int.Parse(Request.QueryString["TourId"]));
            }
            LoadPartnarByPrice();
            if (_calculatorList.Count == 0) return;
            RenderForm();
        }

        public void RenderForm()
        {

            if (!String.IsNullOrEmpty(Request.QueryString["TourId"]))
            {
                InsuranceList = _calculatorList.Where(p => p.ItemType == 7).ToList();
                var insurancePricesAdults = InsuranceList.Sum(p => p.TotalPriceInsuranceChild);
                var insurancePricesChild = InsuranceList.Sum(p => p.TotalPriceInsuranceAdults);
                var insurancePricesBaby = InsuranceList.Sum(p => p.TotalPriceInsuranceBaby);

                TotalInsurancePricesAdults = insurancePricesAdults / tourInfo.QuantityAdult;
                TotalInsurancePricesChild = insurancePricesChild / tourInfo.QuantityChild;
                TotalInsurancePricesBaby = insurancePricesBaby / tourInfo.QuantityBaby;

                TotalInsurancePrices = insurancePricesAdults +
                                       insurancePricesChild +
                                       insurancePricesBaby;
            }
        }

        public void LoadPartnarByPrice()
        {
            ddlPartnerInsurance.DataSource = new PartnerController().ListInsurancePartner().OrderBy(p => p.PartnerName);
            ddlPartnerInsurance.DataValueField = "PartnerId";
            ddlPartnerInsurance.DataTextField = "PartnerName";
            ddlPartnerInsurance.DataBind();

            ddlPartnerInsurance.Items.Insert(0, new ListItem("--Chọn nhà cung cấp--", "-1"));
            
        }
    }
}